//
//  LSZDaoJianTableViewController.m
//  JKFuYinProject
//
//  Created by 九酷 on 2017/12/20.
//  Copyright © 2017年 LC. All rights reserved.
//

#import "LSZDaoJianTableViewController.h"
@interface LSZDaoJianTableViewController ()

@end

@implementation LSZDaoJianTableViewController
{
    /// 数据源数组
    NSArray *_dataSource;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerClass:[LSZDGJZTableViewCell class] forCellReuseIdentifier:@"LSZDGJZTableViewCell"];
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.estimatedRowHeight = 0;
    self.tableView.estimatedSectionFooterHeight =0;
    self.tableView.estimatedSectionHeaderHeight =0;
    [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 20, 0, 20)];
    self.arrayData = [[NSMutableArray alloc]init];

    self.arrayTitle = @[@"祷告词大全", @"基督教祷告词", @"为他人代祷祷告词",@"日常祷告词",@"基督教教会祷告词"];
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"PrayerWords" ofType:@"plist"];
    _ArrayCount = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];
    [self.tableView registerClass:[LSZLibraryTableViewCell class] forCellReuseIdentifier:@"LSZLibraryTableViewCell"];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCellIdentifier"];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(InfoNotificationDgjz:) name:@"dgjz" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(InfoNotificatiofbdgjz) name:@"fbdgjz" object:nil];
}
-(void)InfoNotificatiofbdgjz
{
    self.numberPage = 1;
    [self addDataDic:self.numberPage];
}
-(void)InfoNotificationDgjz:(NSNotification *)notification
{
    NSDictionary * dic = [notification userInfo];
    int numRow = [dic[@"num"] intValue];
    if (![_cid isEqualToString:@"4"]) {
        NSIndexPath *indexPath=[NSIndexPath indexPathForRow:numRow  inSection:0];
        [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
    }else{
        [self.tableView reloadData];
    }
 }
#pragma mark - 传过来的cid  刷新数据
-(void)setCid:(NSString *)cid
{
    _cid = cid;
    if ([_cid isEqualToString:@"4"]) {
        self.tableView.mj_footer.hidden = YES;
        [self.tableView reloadData];
    }else{
        self.tableView.mj_footer.hidden = NO;
        self.numberPage = 1;
        [self addDataDic:self.numberPage];
        @weakify(self)
        self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            @strongify(self)
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                self.numberPage = 1;
                [self addDataDic :self.numberPage];
                [self.tableView.mj_header endRefreshing];
            });
        }];
        self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            @strongify(self)
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                self.numberPage++;
                [self addDataDic:self.numberPage];
                [self.tableView.mj_footer endRefreshing];
            });
        }];
     }
}
-(void)addHuanCUnDataDic :(NSInteger)numberPage
{
    NSString * strid = [NSString stringWithFormat:@"%@_2_%@",shengjing_daogaojianzheng,self.cid];
    @weakify(self)
    [LSZCacheHandling seeIfThereIsCache:@"lsz_gd" :strid :^(BOOL isoK, NSMutableArray *array) {
        @strongify(self)
        if (isoK == YES) {
            LSZPrayerAndTestimonyManager * manager = [[LSZPrayerAndTestimonyManager alloc]initWithPrayerAndTestimonyArray:array];
            [self setDetailsVidelManager:manager :numberPage];
            self.tableView.mj_footer.hidden = NO;
        }else{
         [self setHubStr:NoNetwork];
         self.tableView.mj_footer.hidden = YES;
        }
    }];
}
-(void)addDataDic :(NSInteger)numberPage
{
    @weakify(self)
    [LSZRequestResolutionDic initWithAllPrayersAndTestimonies:_cid page:[NSString stringWithFormat:@"%ld",(long)numberPage] quBie:2 :^(BOOL isOK, NSString * Str, LSZPrayerAndTestimonyManager * manager) {
        @strongify(self)
        if (isOK == YES) {
             [self setDetailsVidelManager:manager :numberPage];
         }else{
             self.numberPage = 1;
             [self addHuanCUnDataDic:self.numberPage];
        }
    }];
}
-(void)setDetailsVidelManager :(LSZPrayerAndTestimonyManager *)mg  :(NSInteger)numberPage
{
    if (numberPage == 1 ) {
        [self.arrayData removeAllObjects];
        [self.arrayData addObjectsFromArray:mg.mutMgArray];
        if(mg.mutMgArray.count == 0){
            self.tableView.mj_footer.hidden = YES;
         }else{
            self.tableView.mj_footer.hidden = NO;
         }
     }else{
        [self.arrayData addObjectsFromArray:mg.mutMgArray];
         if(mg.mutMgArray.count == 0){
             self.tableView.mj_footer.hidden = YES;
             self.numberPage = self.numberPage - 1;
         }else{
             self.tableView.mj_footer.hidden = NO;
         }
     }
    if(mg.mutMgArray.count == 0){
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    }
    [self.tableView reloadData];
}
-(void)setHubStr :(NSString *)str
{
    AlertKuangView * alert = [AlertKuangView new];
    [alert moveJingGaoKuang:str :self.view];
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([_cid isEqualToString:@"4"]) {
        return self.arrayTitle.count;
    }
    return self.arrayData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([_cid isEqualToString:@"4"]) {
        LSZLibraryTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZLibraryTableViewCell" forIndexPath:indexPath];
        cell.labelTotalTitle.hidden = NO;
        cell.labelTotalTitleNum.hidden = NO;
        if (self.arrayTitle.count > indexPath.row) {
            cell.labelTotalTitle.text = [NSString stringWithFormat:@"%@",self.arrayTitle[indexPath.row]];
        }
        if (_ArrayCount.count > indexPath.row) {
            NSArray * array = _ArrayCount[indexPath.row];
            cell.labelTotalTitleNum.text = [NSString stringWithFormat:@"%ld条",(long)array.count];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else{
        LSZDGJZTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LSZDGJZTableViewCell" forIndexPath:indexPath];
        if (self.arrayData.count > indexPath.row) {
            LSZPrayerAndTestimonyMg  * manager = nil;
            manager = self.arrayData[indexPath.row];
            cell.manager = manager;
            if (indexPath.row < 3) {
                cell.labelSelect.hidden = NO;
                CGRect rect = cell.labelTitle.frame;
                rect.origin.x =  CGRectGetMaxX(cell.labelSelect.frame)+5;
                cell.labelTitle.frame = rect;
            }else{
                CGRect rect = cell.labelTitle.frame;
                rect.origin.x = cell.labelSelect.frame.origin.x;
                cell.labelTitle.frame = rect;
                cell.labelSelect.hidden = YES;
            }
            cell.labelType.hidden = YES;
            cell.labelBigType.hidden = YES;
            cell.imageViewYuan.hidden = YES;
            cell.butFocusOn.tag = indexPath.row+1;
            cell.imageViewHeader.tag = indexPath.row+1;
            __weak typeof(self) weakSelf = self;
            [cell.imageViewHeader tapGesture:^(UIGestureRecognizer *ges) {
                LSZPersonalInformationPageVC * vc = [[LSZPersonalInformationPageVC alloc]init];
                vc.uid =manager.uid;
                vc.imageViewHeader = manager.publisher_pic;
                [weakSelf.navigationController pushViewController:vc animated:YES];
            }];
        }
        [cell.butFocusOn addTarget:self action:@selector(clickButFocusOn:) forControlEvents:UIControlEventTouchUpInside];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}
#pragma mark - 点击家关注
-(void)clickButFocusOn:(UIButton *)button
{
    if([[_UserM usercode] isEqualToString:@""]){
        pushAccLoginVC;
    }else{
        LSZPrayerAndTestimonyMg  * manager = nil;
        if (self.arrayData.count>button.tag-1 ) {
            manager = self.arrayData[button.tag-1];
            [LSZRequestData payAttentionToOthers:manager.uid Ck:@"0" mode:@"0" :^(BOOL isOk, NSString * str, NSDictionary * dic) {
                if (isOk == YES) {
                    [self setHubStr:@"关注成功!"];
                    manager.is_follow = @"1";
                    LSZDGJZTableViewCell * cell = (LSZDGJZTableViewCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:button.tag-1  inSection:0]];
                    [cell.butFocusOn setTitle:@"已关注" forState:UIControlStateNormal];
                    cell.butFocusOn.backgroundColor =  partialShallow;
                    cell.butFocusOn.userInteractionEnabled = NO;
                }else{
                    [self setHubStr:str];
                }
            }];
        }
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([_cid isEqualToString:@"4"]) {
        CGFloat height = [LSZSpecialPackaging initWithReturnsthesizeOf:49 :4];
        return height;
    }else{
        LSZPrayerAndTestimonyMg * model = nil;
        model = self.arrayData[indexPath.row];
        CGFloat cellH = [self initWithStrHeigh:model.content];
        CGFloat heightCellH = [LSZSpecialPackaging initWithReturnsthesizeOf:114 :5];
        return (cellH+heightCellH+(ScreenWidth-30)/16*9);
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([_cid isEqualToString:@"4"]) {
        LSZWordBankDetailsVC * vc  = [[LSZWordBankDetailsVC alloc]init];
        vc.strWhat = indexPath.row;
        [self.navigationController pushViewController:vc animated:YES];
    }
    else{
        LSZPrayerAndTestimonyMg  * manager = nil;
        manager = self.arrayData[indexPath.row];
        @weakify(self)
        [LSZSpecialPackaging addPrayerNumber:manager.id_id :^(BOOL isok) {
            @strongify(self)
            if(isok == YES){
                NSString * strid = [NSString stringWithFormat:@"%@_2_%@",shengjing_daogaojianzheng,self.cid];
                 NSInteger num = [manager.browseNum  integerValue]+1;
                manager.browseNum = [NSString  stringWithFormat:@"%ld",(long)num];
                [LSZCacheHandling modifyingParameterOfData:@"lsz_gd" SmallDirectory:strid key:@"id" value:manager.id_id parameter:@"browseNum" modifiedValue:[NSString stringWithFormat:@"%ld",(long)num]];
                 manager.browseNum = [NSString stringWithFormat:@"%ld",(long)num];
            }
        }];
        LSZPrayerContentEvaluationVC * vc = [[LSZPrayerContentEvaluationVC  alloc]init];
        vc.resource_id = manager.id_id;
        if([manager.type isEqualToString:@"0"]){
            if([manager.publisher isEqualToString:@""]||manager.publisher.length == 0){
                vc.strTitle = [NSString stringWithFormat:@"SJ_%@为%@关于%@的祷告",manager.uid,manager.toName,manager.typeName];
            }else{
                vc.strTitle = [NSString stringWithFormat:@"%@为%@关于%@的祷告",manager.publisher,manager.toName,manager.typeName];
            }
        }else{
            if([manager.publisher isEqualToString:@""]||manager.publisher.length == 0){
                vc.strTitle = [NSString stringWithFormat:@"SJ_%@为%@关于%@的见证",manager.uid,manager.title,manager.typeName];
            }else{
                vc.strTitle = [NSString stringWithFormat:@"%@为%@关于%@的见证",manager.publisher,manager.title,manager.typeName];
            }
        }
        vc.imageViewHeader = manager.publisher_pic;
        vc.imageUrl   = manager.imageUrl;
        vc.strName    = manager.publisher;
        vc.strTime    = manager.createTime;
        vc.strtype    = manager.type;
        vc.strtypeName= manager.typeName;
        vc.strContent = manager.content;
        vc.id_id      = manager.id_id;
        vc.uid        = manager.uid;
        vc.lastNumber = (NSInteger)indexPath.row;
        vc.arrayData  = self.arrayData;
        vc.comeFrame  = @"2";
        vc.is_follow = manager.is_follow;
        vc.prayerAndTestimonyMg = manager;
        [self.navigationController pushViewController:vc animated:YES];
    }
}
-(CGFloat)initWithStrHeigh :(NSString * )str
{
    CGFloat fontType = [LSZSpecialPackaging initWithReturnsthesizeOf:14 :0.5];
    int B = [LSZSpecialPackaging initWithReturnsthesizeOf:20 :2];
    int A = B*5;
    NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    style.lineBreakMode = NSLineBreakByWordWrapping;
    style.alignment = NSTextAlignmentLeft;
    style.lineSpacing = 5;
    CGSize maxSize = CGSizeMake(ScreenWidth-30, A);
    NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:fontType], NSParagraphStyleAttributeName:style};
    CGSize labelSize = [str boundingRectWithSize:maxSize options: NSStringDrawingTruncatesLastVisibleLine |  NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
    return labelSize.height+15;
}
@end
